public class MySingleList {
    static class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;//相当于插入进一个空的链表
        }else{
            node.next = head;
            head = node;
        }
    }
    public void display() {
        while(head != null){
            System.out.print(head.val+" ");
            head = head.next;
        }
        System.out.println();
    }
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }//链表为空时，返回空
        if (head.next == null) {
            return head;
        }//链表只有一个节点时，返回本身
        ListNode cur = head.next;
        head.next = null;

        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }
}
